<?php
/**
 * 目录管理
 * @since   2018-01-16
 */

namespace app\admin\controller;


use app\common\model\CommonClientVersionManage as upgradeModel;
use app\util\Tools;

class ClientVersion extends Base {

    /**
     * @OA\Get(
     *   path="/admin/ClientVersion/index",
     *   tags={"客户端相关"},
     *   summary="版本升级管理",
     *   @OA\Parameter(name="type",in="query",required=true,description="检索类型",@OA\Schema( type="string" )),
     *   @OA\Parameter(name="keywords",in="query",required=true,description="关键词",@OA\Schema( type="string" )),
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Parameter(ref="#/components/parameters/size"),
     *   @OA\Parameter(ref="#/components/parameters/page"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON", 
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *         example={"code":1,"msg":"获取成功","data":{"list":{{"id":19,"uid":0,"member_id":0,"original_name":"login-bg.jpg","path":"default/20200109\\5b36c263d0e17f612b2f8c0f740cef8e.jpg","type":"image/jpeg","size":460261,"hash":"d7f4dd4f55ca9dd194cd80927119e167f137fa9f","create_time":"2020-01-09 22:00:50"}},"count":1}}
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function index() {
        $limit   = $this->request->get('size', config('easyApi.ADMIN_LIST_DEFAULT'));
        $start   = $this->request->get('page', 1);
        $type    = $this->request->get('type');
        $keywords= $this->request->get('keywords');
        $obj = new upgradeModel();
        if($type=='name'){
            $obj = $obj->where(['name'=>$keywords]);
        }
        if($type=='app_id'){
            $obj = $obj->where(['app_id'=>$keywords]);
        }
        $list    = $obj->order('create_time desc')->paginate($limit, false, ['page' => $start])->toArray();
        $result  = $obj->field('app_id,name,version,wgt_url,update_url,describe')->order('create_time desc')->select()->toArray();

        $appList = [];
        foreach ($result as $val){
            if(!isset($appList[$val['app_id']])){
                $appList[$val['app_id']] = $val;
            }
        }
        $this->success('获取成功',[
            'list'  => $list['data'],
            'count' => $list['total'],
            'all_app_list' => $appList
        ]);
    }

    /**
     * @OA\Post(
     *   path="/admin/ClientVersion/add",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   tags={"客户端相关"},
     *   summary="新增版本更新",
     *   @OA\RequestBody(
     *      @OA\MediaType(
     *         mediaType="application/json",
     *         @OA\Schema(
     *            @OA\Property(property="app_id", type="string", description="标识"),
     *            @OA\Property(property="name", type="string", description="名称"),
     *            @OA\Property(property="version", type="string", description="版本号"),
     *            @OA\Property(property="wgt_url", type="string", description="增量包更新地址"),
     *            @OA\Property(property="update_url", type="string", description="整包更新地址"),
     *            @OA\Property(property="describe", type="string", description="更新内容描述"),
     *            example={"app_id": "com.myapp.www", "name": "爱你一万年","version":"1.0.0","wgt_url":"http://www.qiniu.com/cgi/5d593c26075d423e15d7fff34cef7461/os.wgt","update_url":"http://www.qiniu.com/cgi/5d593c26075d423e15d7fff34cef7461/os.apk","describe":"修复了若干BUG，提升稳定性。"}
     *         )
     *       )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK",
     *         @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *     ),
     *     @OA\Response(response="500", description="服务器错误"),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function add() {
        $postData = $this->request->post();
        if(empty($postData['app_id'])){
            $this->error("缺少标识参数");
        }
        if(empty($postData['name'])){
            $this->error("缺少名称参数");
        }
        if(empty($postData['version'])){
            $this->error("缺少版本号参数");
        }
        if(empty($postData['wgt_url']) && empty($val['update_url'])){
            $this->error("至少要有个更新包地址");
        }
        $find = upgradeModel::where(['app_id'=>$postData['app_id']])->order('create_time desc')->find();
        if(!empty($find)){
           if(str_replace(".", "", $postData['version']) <= str_replace(".", "", $find['version'])){
               $this->error("版本号必须大于上个版本号");
           }
        }
        $res = upgradeModel::create($postData);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Put(
     *     path="/admin/ClientVersion/changeStatus/{id}",
     *     tags={"客户端相关"},
     *     summary="更新状态",
     *     @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *     @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *     @OA\Parameter(name="id",in="path",required=true,description="版本记录ID",@OA\Schema( type="integer", format="int32", minimum=1.0 )),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property( property="status", type="string", description="状态。0关闭更新，1开启更新" ),
     *                 example={"status": 1}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK"
     *     ),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function changeStatus($id) {
        $status = $this->request->param('status');
        $res = upgradeModel::update([
            'id'   => $id,
            'status' => $status
        ]);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Put(
     *     path="/admin/ClientVersion/changeForce/{id}",
     *     tags={"客户端相关"},
     *     summary="强制更新",
     *     @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *     @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *     @OA\Parameter(name="id",in="path",required=true,description="版本记录ID",@OA\Schema( type="integer", format="int32", minimum=1.0 )),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property( property="force", type="string", description="状态。0开启强制更新，1关闭强制更新" ),
     *                 example={"force": 1}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK"
     *     ),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function changeForce($id) {
        $force = $this->request->param('force');
        $res = upgradeModel::update([
            'id'   => $id,
            'is_force' => $force
        ]);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Delete(path="/admin/ClientVersion/delete/{id}",
     *   tags={"客户端相关"},
     *   summary="删除版本号",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Response(
     *     response=200,
     *     description="返回Json",
     *     @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *   ),
     *   @OA\Response(response=500, description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function delete($id) {
        if (!$id) {
            $this->error('缺少必要参数');
        }
        $result = upgradeModel::destroy($id);
        if (!$id) {
            $this->error('删除失败');
        }
        $this->success();
    }

}
